package com.nuanshui.heatedloan.extension.page;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.nuanshui.heatedloan.pageable.Pager;

public class PageProcessor implements HandlerMethodArgumentResolver{

	/**
	 * 支持的请求参数封装的类型，这里是处理参数为Pager的
	 */
	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return Pager.class.isAssignableFrom(parameter.getParameterType());
	}

	@Override
	public Pager resolveArgument(MethodParameter parameter,
			ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
			WebDataBinderFactory binderFactory) throws Exception {
		String pageNum = webRequest.getParameter("pageNum");
		String numPerPage = webRequest.getParameter("numPerPage");
		//要对pageNum和numPerPage进行空判断，防止出现异常
		Pager pager=null ;
		if(StringUtils.isNotEmpty(pageNum)&&StringUtils.isNotEmpty(numPerPage)){
			pager= new Pager(Integer.parseInt(pageNum),Integer.parseInt(numPerPage));
		}
		return pager;
	}
	
}
